package org.budo.druid.util;

import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.util.JdbcUtils;

/**
 * @author lmw
 */
public class DruidUtil {
    public static String getDbType(DataSourceProxy dataSource) {
        String dbType = dataSource.getDbType();

        if (null != dbType && !dbType.isEmpty() && !"null".equalsIgnoreCase(dbType)) {
            return dbType;
        }

        dbType = JdbcUtils.getDbType(dataSource.getRawJdbcUrl(), null);
        if (null != dbType && !dbType.isEmpty() && !"null".equalsIgnoreCase(dbType)) {
            return dbType;
        }

        dbType = JdbcUtils.getDbType(dataSource.getUrl(), null);
        return dbType;
    }

    public static String toString(DataSourceProxy dataSource) {
        if (null == dataSource) {
            return null;
        }

        return dataSource + ", url=" + dataSource.getUrl() + ", rawJdbcUrl=" + dataSource.getRawJdbcUrl();
    }
}